package com.cuiweiyou.vigilesmicrecord.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by macpro on 2018/4/18.
 */

public class VolumeWaveView extends View {

    private Paint paint;
    private float strokeWidth = 1f;    // 笔触宽度
    private List<Double> volumeList;     // 分贝集合

    public VolumeWaveView(Context context) {
        super(context);
        init();
    }

    public VolumeWaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VolumeWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        int foregroundColour = Color.parseColor("#b3d7ff");

        paint = new Paint();
        paint.setColor(foregroundColour);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(strokeWidth);
        paint.setStyle(Paint.Style.STROKE);

        volumeList = new ArrayList<>();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawVolume(canvas);
    }

    public void setVolume(double volume) {
//        Log.i("ard", "分贝：" + volume);

        if (-1 == volume) {
            volumeList.clear();
            invalidate();
            return;
        }

        int height = getHeight();

        volume = volume * 100;
        volume = volume % height; // 不太精确的取差值

        if (volumeList.size() > 0) {
            this.volumeList.add(volumeList.size() - 1, volume);
        } else {
            this.volumeList.add(volume);
        }

        this.volumeList.add(volume); // 冗余宽度
        this.volumeList.add(volume);
        this.volumeList.add(0d);

        float volumeCountWidth = volumeList.size() * strokeWidth;
        int viewWidth = getWidth();
        if (volumeCountWidth > viewWidth) { // 如果全部绘制数据宽度超出了View的宽度就去除旧数据，达到滚动绘制效果
            do {
                volumeList.remove(0);
                volumeCountWidth = volumeList.size() * strokeWidth;
            } while (volumeCountWidth > viewWidth);
        }

        invalidate();
    }

    private void drawVolume(Canvas canvas) {
        Path path = new Path();
        float height = getHeight();
        float width = getWidth();
        float size = volumeList.size();
        float volumeCountWidth = size * strokeWidth;
        float diffWidth = 0;
        if (volumeCountWidth < width) {
            diffWidth = width - volumeCountWidth; // 不想从view中心开始绘制-从左边开始，就注释掉
            diffWidth = diffWidth / 2;
        }

        for (int i = 0; i < size; i++) {
            if (0 == i % 2) {
                //continue; // 间隔绘制
            }

            double volume = volumeList.get(i);

            double top = height / 2 - volume;
            if (top < 0) {
                double tmp = volume;
                do {
                    tmp = tmp / 4 * 3;
                    top = height / 2 - tmp;
                } while (top < 0);
            }

            double bottom = height / 2 + volume;
            if (bottom > height) {
                double tmp = volume;
                do {
                    tmp = tmp / 4 * 3;
                    bottom = height / 2 + tmp;
                } while (bottom > height);
            }

            path.moveTo(i * strokeWidth + diffWidth, (float) (top));
            path.lineTo(i * strokeWidth + diffWidth, (float) (bottom));
        }

        path.moveTo(0, height / 2);
        path.lineTo(width, height / 2);

        canvas.drawPath(path, paint);
    }

    public Handler getWaveHandler() {
        return waveHandler;
    }

    private Handler waveHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            double volume = (double) msg.obj;
            setVolume(volume);
        }
    };
}


